const WxFly = require('../../lib/wxplain.js');

Page({
  data: {
    modalHidden: false,
    score: '0',
    imageNumber: 0,
    discover: false,
    isPause: false,
    isPlay: true,
    isMute: false,
    timescore: 0,
    number: 0,
    isSee: true
  },
  onLoad: function (options) {
    let that = this
    // 页面初始化 options为页面跳转所带来的参数
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          width: res.screenWidth,
          height: res.screenHeight
        })
      },
    })
    const fly = this.fly = new WxFly(
      {
        ctx: wx.createCanvasContext(),
        id: 'plain',
        height: that.data.height,
        width: that.data.width,
      });
    fly.on('over', packet => {
      let gameover = wx.createInnerAudioContext()
      gameover.src = 'https://resource.160nurse.com/canvas/healthy/mp3/gameover.mp3'
      if (packet.isEnd == 1) {
        gameover.autoplay = false
      } else {
        gameover.autoplay = true
      }

      gameover.onPlay(() => {

      })
      gameover.onEnded(() => {
        gameover.destroy()
        if (packet.isEnd == 0) {
          wx.redirectTo({
            url: 'end?score=' + packet.score + '&gold=' + packet.gold + '&red=' + packet.red,
            // url: '../index/index'
          })
          fly.removeAllListeners()
        }
      })
    });
  },
  onReady: function () {
    // 页面渲染完成
   
  },
  startGame: function () {
    const fly = this.fly;
    this.setData({ score: 0, modalHidden: false });
    fly.startGame();
  },
  move: function (event) {
    const fly = this.fly;
    var x = event.touches[0].x;
    var y = event.touches[0].y;
    fly.touchmove(x, y);
  },
  click: function () {
    let that = this
    const fly = this.fly;
    if (!that.data.isPause) {
      that.setData({
        isPause: true
      })
    } else {
      that.setData({
        isPause: false
      })
    }
    fly.touchclick();
  },
  pauseAudio () {
    let that = this
    const fly = this.fly;
    if (!that.data.isMute) {
      that.setData({
        isMute: true
      })
    } else {
      that.setData({
        isMute: false
      })
    }
    fly.pauseAudio();
  },
  onShow: function () {
    let that = this
    if (that.data.number == 1) {
      console.log('跳转', that.data.number)
      wx.redirectTo({
        url: 'index?link=isredirect',
      })
    }
    
    // const fly = this.fly;
    // let that = this
    // console.log('打开', that.data.number)
    // that.setData({
    //   discover: true
    // })
  },
  onHide: function () {
    // 页面隐藏
    let that = this
    // fly.over()
    const fly = this.fly
    that.setData({
      number: 1,
      isSee: false
    })
    fly.over()

  },
  onUnload: function () {
    // 页面关闭
    const fly = this.fly
    console.log(this.fly)
  }
})
